/*
	Anonymous Image Board Image Downloader - program for downloading pictures from AIB
	Copyright (C) 2011-2012 Metallhead <Metallhead@megabox.ru>

	HomePage:    http://code.google.com/p/aibid/

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "mainwindow.h"

#include <QSettings>
#include <QFile>
#include <QModelIndex>
#include <QMessageBox>
#include <QDesktopServices>
#include <QMenu>
#include <QDebug>
#include <QProcess>
#include <QIcon>
#include <QHeaderView>
#include <QFileDialog>
#include <QResizeEvent>

#ifdef Q_OS_WIN
#include "qt_windows.h"
#include "qwindowdefs_win.h"
#include <shellapi.h>
#endif

MainWindow::MainWindow(QString applicationName, QWidget *parent) :
	QMainWindow(parent),
	ui(new Ui::MainWindow)
{
	ui->setupUi(this);
	set = new settingsDialog(this);
	fb = new FeedBack(this);
	tray = new QSystemTrayIcon(this);
	tray->setToolTip("AIBID");
	QMenu *trayMenu = new QMenu(this);
	QAction * trayExit = new QAction(tr("Exit"), this);
	connect(trayExit,SIGNAL(triggered()),this,SLOT(exitProgram()));
	QAction * trayStop = new QAction(tr("Stop all"), this);
	connect(trayStop,SIGNAL(triggered()),this,SLOT(stopAllDownloads()));
	QAction * trayResume = new QAction(tr("Resume all"), this);
	connect(trayResume,SIGNAL(triggered()),this,SLOT(resumeAllDownloads()));
	QAction * trayDelete = new QAction(tr("Delete all"), this);
	connect(trayResume,SIGNAL(triggered()),this,SLOT(deleteAllDownloads()));

	trayMenu->addAction(trayResume);
	trayMenu->addAction(trayStop);
	trayMenu->addAction(trayDelete);
	trayMenu->addAction(trayExit);
	tray->setContextMenu(trayMenu);
	connect(tray,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(showHide(QSystemTrayIcon::ActivationReason)));
	tray->setIcon(QIcon(":/icon.ico"));
	set->tray = tray;

	appName = applicationName;

	connect(ui->actionOpen_settings,SIGNAL(triggered()),this,SLOT(openSettings_clicked()));
	connect(set,SIGNAL(saveThreadsOn()),this,SLOT(saveAllThreadsToRegistry()));
	connect(set,SIGNAL(saveThreadsOff()),this,SLOT(deleteAllThreadsFromRegistry()));

	model.setRowCount(0);
	model.setColumnCount(4);
    ui->tableView->setModel(&model);
    ui->tableView->setItemDelegate(&delegate);
    QStringList headers;
	headers<< tr("thread") << tr("progress") << tr("path") << tr("status");
	model.setHorizontalHeaderLabels(headers);
	ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);

	loadColumnsSizes();
	connect(ui->tableView->horizontalHeader(),SIGNAL(sectionResized(int,int,int)),this,SLOT(columnSizeChanged(int,int,int)));

	QSettings settings(QCoreApplication::organizationName(),QCoreApplication::applicationName());
	if(settings.value("settings/tray").toBool())
	{
		tray->show();
		qDebug() << "tray showed";
	}

	connectionsOpened = 0;
	Common::createParserIfNotExists();
	detectors = Common::readDetectors(settings.value("settings/parser").toString());

	if(settings.value("settings/rememberThreads").toBool())
	{
		loadThreads(settings);
		qDebug() << "threads loaded from registry (or somewhere else)";
	}

	ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);

	xE.setModal(true);

	Common::deleteLogs();

	this->resize(settings.value("settings/width").toInt(),settings.value("settings/height").toInt());
	QString windowState = settings.value("settings/windowState").toString();
	if(windowState == "normal")
		this->setWindowState(Qt::WindowNoState);
	if(windowState == "maximized")
		this->setWindowState(Qt::WindowMaximized);

	QApplication::setWindowIcon(QIcon(":/icon.ico"));


#ifndef Q_OS_LINUX
	qDebug() << "trying to check new version";
	checkNewVersion();
#endif
}

MainWindow::~MainWindow()
{
	delete tray;
	delete set;
	delete fb;
	qDeleteAll(threads);
	delete ui;
}
void MainWindow::exitProgram()
{
	QCoreApplication::exit();
}

void MainWindow::on_addThread_clicked()
{
	QString site, path;
	int updateTime, threadsAmount;
	imageTypes alowedTypes;
	add = new addDialog(this, &site, &path, &updateTime, &threadsAmount, &alowedTypes);
	add->exec();

	if(add->result()==QDialog::Accepted)
		addThread(site, path,updateTime, threadsAmount, QDateTime::currentDateTime(), false, alowedTypes);
	delete add;
}

void MainWindow::openSettings_clicked()
{
	set->exec();
}

void MainWindow::on_deleteThread_clicked()
{
	deleteThread(ui->tableView->currentIndex().row());
}

void MainWindow::on_openFolder_clicked()
{
	QSettings settings(QCoreApplication::organizationName(),QCoreApplication::applicationName());
    if(ui->tableView->currentIndex().row()==-1)
                QDesktopServices::openUrl(QUrl::fromLocalFile(settings.value("settings/path").toString()));
    else
		QDesktopServices::openUrl(QUrl::fromLocalFile(threads.at(ui->tableView->currentIndex().row())->getPath()));
}

void MainWindow::on_stopThread_clicked()
{
    if(ui->tableView->currentIndex().row()==-1)
		return;
	threads.at(ui->tableView->currentIndex().row())->stopThread();
}

void MainWindow::on_resumeThread_clicked()
{
	if(ui->tableView->currentIndex().row()==-1)
		return;
	threads.at(ui->tableView->currentIndex().row())->resumeThread();
}

void MainWindow::showHide(QSystemTrayIcon::ActivationReason r)
{
	if (r==QSystemTrayIcon::Trigger)
	{
		if (!this->isVisible())
		{
			qDebug() << "showing tray";
			this->showNormal();
			this->activateWindow();
		}
		else
		{
			qDebug() << "hiding tray";
			this->hide();
		}
	}
}

void MainWindow::deleteThread(int row)
{
	QSettings settings(QCoreApplication::organizationName(),QCoreApplication::applicationName());
	if(row==-1)
		return;

	for(int i=0;i<model.rowCount(QModelIndex());i++)
		threads.at(i)->lock.lockForWrite();

	int rowIndex = row;

	downloads * deleted = threads[rowIndex];
	threads.at(rowIndex)->stopThread();
	threads.at(rowIndex)->lock.unlock();

	threads.remove(rowIndex,1);

	settings.setValue("threads/"+QString::number(threads.count())+"/active",QVariant(false));

	model.removeRow(rowIndex,QModelIndex());

	for(int i=rowIndex;i<model.rowCount(QModelIndex());i++)
	{
		QModelIndex index = model.index(i,1,QModelIndex());
		threads.at(i)->setIndex(index.row());
		settings.setValue("threads/"+QString::number(i)+"/url",QVariant(settings.value("threads/"+QString::number(i+1)+"/url")));
		settings.setValue("threads/"+QString::number(i)+"/path",QVariant(settings.value("threads/"+QString::number(i+1)+"/path")));
		settings.setValue("threads/"+QString::number(i)+"/addititonTime",QVariant(settings.value("threads/"+QString::number(i+1)+"/additionTime")));
		settings.setValue("threads/"+QString::number(i)+"/threadsLimit",QVariant(settings.value("threads/"+QString::number(i+1)+"/threadsLimit")));
		settings.setValue("threads/"+QString::number(i)+"/updateTime",QVariant(settings.value("threads/"+QString::number(i+1)+"/updateTime")));
	}

	settings.setValue("threads/count",QVariant(threads.count()));

	for(int i=0;i<model.rowCount(QModelIndex());i++)
		threads.at(i)->lock.unlock();
	delete deleted;
}

void MainWindow::updateProgressBar(int index)
{
	QModelIndex ind = model.index(index,1);
	model.setData(ind, QVariant(threads.at(index)->getPercent()));
	ind = model.index(index,3);
	if(threads.at(index)->isTimerActive())
	{
		int timeRemains = threads.at(index)->timeUntilUpdate();
		int imagesDownloaded = threads.at(index)->getImagesDownloaded();
		int imageCount = threads.at(index)->getImagesFound();
		QString format;
		if((timeRemains)/3600)
			format = "hh:mm:ss";
		else
			format = "mm:ss";
		QTime time((timeRemains)/3600,((timeRemains)/60)%60,(timeRemains)%60);
		QString str = QString(tr("Awaiting ") + time.toString(format) + tr(", images downloaded: ") + QString::number(imagesDownloaded) + "/" + QString::number(imageCount));
		model.setData(ind, QVariant(str));
	}
	else
		model.setData(ind, QVariant(threads.at(index)->getStatus()));
	ui->tableView->resizeColumnToContents(3);
}

void MainWindow::stopAllDownloads()
{
	foreach(downloads * down, threads)
	{
		down->stopThread();
	}
}

void MainWindow::resumeAllDownloads()
{
	foreach(downloads * down, threads)
	{
		down->resumeThread();
	}
}

void MainWindow::deleteAllDownloads()
{
    while (model.rowCount() != 0)
    {
        deleteThread(0);
    }
}
#ifndef Q_OS_LINUX
void MainWindow::checkNewVersion()
{
	downloadAddress = "ftp://aibid.ru/aibid/";
#ifdef Q_OS_WIN
	QFile file(Common::filePlace("files.lst"));
#endif
#ifdef Q_OS_MAC
	QFile file(Common::packagePlace("mac.ver"));
#endif
	if(!file.exists())
	{
		qDebug() << "file files.lst is lost";
		QDir dir(Common::filePlace(""));
		qDebug() << dir.entryList();
		file.open(QIODevice::WriteOnly);
		file.close();
	}
	else
	{
		file.open(QIODevice::ReadOnly);
		QByteArray data = file.readAll();
		file.close();
		QString currentVersion = QString(data);
		version = currentVersion.toDouble();
		qDebug() << "current version is:" << version;

		qDebug() << "trying to download version file from server";
		QNetworkAccessManager * manager = new QNetworkAccessManager(this);
		connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(versionInfoDownloadFinished(QNetworkReply*)));
#ifdef Q_OS_WIN
		manager->get(QNetworkRequest(QUrl(downloadAddress + "files.lst")));
#endif
#ifdef Q_OS_MAC
		manager->get(QNetworkRequest(QUrl(downloadAddress + "mac.ver")));
#endif
	}
}
#endif //!Q_OS_LINUX

void MainWindow::versionInfoDownloadFinished(QNetworkReply *reply)
{
	QByteArray data = reply->readAll();
#ifdef Q_OS_MAC
	QString newVersion = QString(data);
#endif
	//versionToDownload = newVersion.toDouble();
	//qDebug() << "current server version:" << versionToDownload;
	QFile currentFile(Common::filePlace(reply->url().toString().split("/").last()));
	currentFile.open(QIODevice::ReadOnly);
	QByteArray currentData = currentFile.readAll();
	currentFile.close();
	uint version = qHash(currentData);
	uint versionToDownload = qHash(data);
	if(version != versionToDownload)
	{
		QMessageBox box;
		box.setWindowTitle(tr("Information"));
		box.setText(QString(tr("A newer version is avaiable. Do you want to download?")));
		box.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
		box.setDefaultButton(QMessageBox::No);
		int res = box.exec();
		switch(res)
		{
		case QMessageBox::Yes:
			qDebug() << "starting updater";
			if(appName.contains("exe"))
			{
#ifdef Q_OS_WIN
				QString updaterName = QDir::toNativeSeparators(QCoreApplication::applicationDirPath() + QDir::separator() + "aibid-updater.exe");
				QString runAs = "runas";
				ShellExecute(0, reinterpret_cast<const WCHAR*>(runAs.utf16()), reinterpret_cast<const WCHAR*>(updaterName.utf16()), reinterpret_cast<const WCHAR*>(appName.utf16()), 0, SW_NORMAL);
#endif
				exitProgram();
			}
			else
			{
#ifdef Q_OS_MAC
				QNetworkAccessManager * manager = new QNetworkAccessManager(this);
				connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadMacFile(QNetworkReply*)));
				manager->get(QNetworkRequest(QUrl(downloadAddress + newVersion + "/" + QCoreApplication::applicationName())));
#endif
			}
			break;
		case QMessageBox::No:
			break;
		}
	}
	else
	{
#ifdef Q_OS_MAC
		QFile file(QCoreApplication::applicationName()+"_old");
		if(file.exists())
			file.remove();
#endif
	}
}

void MainWindow::addThread(QString site, QString path, int updateTime, int threadsAmount, QDateTime additionTime, bool loading, imageTypes alowedTypes)
{
	qDebug() << "trying to add thread" << site << "in path" << path;
	QSettings settings(QCoreApplication::organizationName(),QCoreApplication::applicationName());
	for(int i=0;i<model.rowCount(QModelIndex());i++)
	{
		QModelIndex index = model.index(i, 0, QModelIndex());
		if(model.data(index).toString()==site)
		{
			qDebug() << "thread is already in list";
			qDebug() << "thread list:";
			for(int j=0;j<model.rowCount(QModelIndex());j++)
			{
                                QModelIndex ind = model.index(j, 0, QModelIndex());
				qDebug() << j << ":" << model.data(ind).toString();
			}
			QMessageBox * mes = new QMessageBox(this);
			mes->setWindowTitle(tr("error"));
			mes->setText(tr("threadExists"));
			mes->exec();
			return;
		}
	}

	model.setRowCount(model.rowCount()+1);
	qDebug() << "row count now:" << model.rowCount();

	QModelIndex index = model.index(model.rowCount()-1, 0, QModelIndex());
	model.setData(index, QVariant(site));
	index = model.index(model.rowCount()-1, 1, QModelIndex());
	model.setData(index, QVariant("0"));
	index = model.index(model.rowCount()-1,2,QModelIndex());
	model.setData(index,QVariant(path));

	int inde = model.rowCount()-1;

	bool hash = settings.value("settings/hashCheck").toBool();
	qDebug() << "hashCode:" << hash;
	qDebug() << "adding new thread to downloading class";

	QDir dir(path);
	if(!dir.exists())
		dir.mkpath(path);

	downloads * down = new downloads(site, path, inde, hash, threadsAmount, updateTime, additionTime, loading, alowedTypes);

	threads.append(down);
	connect(down, SIGNAL(infoChanged(int)), this, SLOT(updateProgressBar(int)));
	down->downloadThread();
}

void MainWindow::loadThreads(QSettings & settings)
{
	//QSettings settings(QCoreApplication::organizationName(),QCoreApplication::applicationName());
	for(int i=0;i<settings.value("threads/count").toInt();i++)
	{

		if(settings.value("threads/"+QString::number(i)+"/threadsLimit").toString()=="")
			settings.setValue("threads/"+QString::number(i)+"/threadsLimit",settings.value("settings/threadsLimit").toInt());
		if(settings.value("threads/"+QString::number(i)+"/updateTime").toString()=="")
			settings.setValue("threads/"+QString::number(i)+"/updateTime",settings.value("settings/time").toInt());
		if(settings.value("threads/"+QString::number(i)+"/additionTime").toString()=="")
			settings.setValue("threads/"+QString::number(i)+"/additionTime",QDateTime::currentDateTime());
		if(settings.value("threads/"+QString::number(i)+"/jpg").toString()=="")
			settings.setValue("threads/"+QString::number(i)+"/jpg",true);
		if(settings.value("threads/"+QString::number(i)+"/png").toString()=="")
			settings.setValue("threads/"+QString::number(i)+"/png",true);
		if(settings.value("threads/"+QString::number(i)+"/gif").toString()=="")
			settings.setValue("threads/"+QString::number(i)+"/gif",true);


		QString url = settings.value("threads/"+QString::number(i)+"/url").toString();
		QString path = settings.value("threads/"+QString::number(i)+"/path").toString();
		int updateInterval = settings.value("threads/"+QString::number(i)+"/updateTime").toInt();
		int threadsCount = settings.value("threads/"+QString::number(i)+"/threadsLimit").toInt();
		QDateTime additionTime = settings.value("threads/"+QString::number(i)+"/additionTime").toDateTime();
		imageTypes alowedTypes;
		alowedTypes.gif = settings.value("threads/"+QString::number(i)+"/gif").toBool();
		alowedTypes.png = settings.value("threads/"+QString::number(i)+"/png").toBool();
		alowedTypes.jpg = settings.value("threads/"+QString::number(i)+"/jpg").toBool();

		addThread(url, path, updateInterval, threadsCount, additionTime, true, alowedTypes);
	}
}

void MainWindow::saveAllThreadsToRegistry(QSettings & settings)
{
	//QSettings settings(QCoreApplication::organizationName(),QCoreApplication::applicationName());
	for(int i=0;i<threads.count();i++)
	{
		settings.setValue("threads/"+QString::number(i)+"/url", QVariant(threads.at(i)->getUrl()));
		settings.setValue("threads/"+QString::number(i)+"/path",QVariant(threads.at(i)->getPath()));
		settings.setValue("threads/"+QString::number(i)+"/threadsLimit",QVariant(threads.at(i)->getThreadsCount()));
		settings.setValue("threads/"+QString::number(i)+"/updateTime",QVariant(threads.at(i)->getUpdateInterval()));
		settings.setValue("threads/"+QString::number(i)+"/additionTime",QVariant(threads.at(i)->getAdditionTime()));
		settings.setValue("threads/"+QString::number(i)+"/active",QVariant(true));
		imageTypes types = threads.at(i)->getAlowedTypes();
		settings.setValue("threads/"+QString::number(i)+"/jpg",QVariant(types.jpg));
		settings.setValue("threads/"+QString::number(i)+"/png",QVariant(types.png));
		settings.setValue("threads/"+QString::number(i)+"/gif",QVariant(types.gif));
	}
	settings.setValue("threads/count",QVariant(threads.count()));
}

void MainWindow::saveAllThreadsToRegistry()
{
	QSettings settings(QCoreApplication::organizationName(),QCoreApplication::applicationName());
	saveAllThreadsToRegistry(settings);
}

void MainWindow::deleteAllThreadsFromRegistry()
{
	QSettings settings(QCoreApplication::organizationName(),QCoreApplication::applicationName());
	for(int i=0;i<settings.value("threads/count").toInt();i++)
		settings.setValue("threads/"+QString::number(i)+"/active",QVariant(false));
	settings.setValue("threads/count",QVariant(0));
}

void MainWindow::on_actionExit_triggered()
{
	qDebug() << "program closed";
	tray->hide();
	exitProgram();
}

void MainWindow::on_actionFeedback_triggered()
{
	fb->exec();
}


void MainWindow::on_tableView_doubleClicked(const QModelIndex &index)
{
	if(index.row()==-1)
		return;
	QString url = threads.at(index.row())->getUrl();
	QString path = threads.at(index.row())->getPath();
	QString status = threads.at(index.row())->getStatus();
	int tR = threads.at(index.row())->timeUntilUpdate();
	int iD = threads.at(index.row())->getImagesDownloaded();
	int iC = threads.at(index.row())->getImagesFound();
	QDateTime aD = threads.at(index.row())->getAdditionTime();
	int iIF = threads.at(index.row())->getImagesCountInFolder();
	int tC = threads.at(index.row())->getThreadsCount();
	int uI = threads.at(index.row())->getUpdateInterval();
	info = new threadInfo(url,path,status,tR,iD,iC,aD,iIF,tC,uI,this);
	connect(threads.at(index.row()),SIGNAL(infoChanged(QString,int)),info,SLOT(updateInfo(QString,int)));
	connect(threads.at(index.row()),SIGNAL(infoChanged(bool,int,int)),info,SLOT(updateInfo(bool,int,int)));
	info->setModal(true);
	info->show();
}

void MainWindow::on_tableView_customContextMenuRequested(const QPoint &pos)
{
	if(ui->tableView->rowAt(pos.y())!=-1)
	{
		QMenu menu;
		menu.addAction(tr("Copy url"));
		menu.addAction(tr("Open in browser"));
		connect(menu.actions().at(0),SIGNAL(triggered()),this,SLOT(copyUrlToClipboard()));
		connect(menu.actions().at(1),SIGNAL(triggered()),this,SLOT(openThreadInBrowser()));
		menu.exec(ui->tableView->mapToGlobal(pos));
		disconnect(menu.actions().at(0),SIGNAL(triggered()),this,SLOT(copyUrlToClipboard()));
		disconnect(menu.actions().at(1),SIGNAL(triggered()),this,SLOT(openThreadInBrowser()));
	}
}

void MainWindow::copyUrlToClipboard()
{
	QApplication::clipboard()->setText(threads.at(ui->tableView->currentIndex().row())->getUrl());
}

void MainWindow::openThreadInBrowser()
{
	QDesktopServices::openUrl(QUrl(threads.at(ui->tableView->currentIndex().row())->getUrl()));
}

void MainWindow::on_actionOpenDetectorsEditor_triggered()
{
	xE.show();
}

void MainWindow::closeEvent(QCloseEvent *q)
{
	QSettings settings(QCoreApplication::organizationName(),QCoreApplication::applicationName());
	bool hideAct = settings.value("settings/close").toBool();
	if (hideAct)
	{
		this->hide();
		q->ignore();
		return;
	}
}

void MainWindow::resizeEvent(QResizeEvent *q)
{
	qDebug() << "!!";
	QSettings settings(QCoreApplication::organizationName(),QCoreApplication::applicationName());
	switch(this->windowState())
	{
	case Qt::WindowMaximized:
		settings.setValue("settings/windowState","maximized");
		break;
	case Qt::WindowNoState:
		settings.setValue("settings/windowState","normal");
		settings.setValue("settings/width",this->width());
		settings.setValue("settings/height",this->height());
		break;
	}
}

void MainWindow::columnSizeChanged(int columnIndex, int, int newSize)
{
	if(columnIndex!=3) //status field
	{
		QSettings settings(QCoreApplication::organizationName(),QCoreApplication::applicationName());
		settings.setValue("columns/"+QString::number(columnIndex)+"/size",QVariant(newSize));
	}
}

void MainWindow::loadColumnsSizes()
{
	QSettings settings(QCoreApplication::organizationName(),QCoreApplication::applicationName());
	for(int i=0;i<model.columnCount()-1;i++) //except status
	{
		if(settings.value("columns/"+QString::number(i)+"/size").toString()!="")
			ui->tableView->setColumnWidth(i, settings.value("columns/"+QString::number(i)+"/size").toInt());
	}
}

#ifdef Q_OS_MAC
void MainWindow::downloadMacFile(QNetworkReply* reply)
{
	QString name = QCoreApplication::applicationName();
	QByteArray array = reply->readAll();
	qDebug() << "mac file downloaded, size: " << array.size();
	QFile::rename(name,name+"_old");
	QFile file(name);
	file.open(QIODevice::WriteOnly);
	file.write(array);
	file.close();
	QProcess * process;
	process->start(name);
	process->waitForStarted();
	exitProgram();
}
#endif

void MainWindow::on_actionExport_list_triggered()
{
	QString fileName = QFileDialog::getSaveFileName(this,tr("Please, specify file name"),QDesktopServices::storageLocation(QDesktopServices::HomeLocation),"*.aib");
	if(fileName == "")
		return;
	if(!fileName.endsWith(".aib"))
		fileName+=".aib";
	QSettings settings(fileName,QSettings::IniFormat);
	saveAllThreadsToRegistry(settings);
	QMessageBox box;
	box.setText(tr("Thread list was successfully exported"));
	box.setWindowTitle(tr("Export"));
	box.exec();
}

void MainWindow::on_actionImport_list_triggered()
{
	QString fileName = QFileDialog::getOpenFileName(this, tr("Please, select file"),QDesktopServices::storageLocation(QDesktopServices::HomeLocation),"*.aib");
	if(fileName == "")
		return;
	QSettings settings(fileName,QSettings::IniFormat);
	loadThreads(settings);
	QMessageBox box;
	box.setText(tr("Thread list was successfully imported"));
	box.setWindowTitle(tr("Import"));
	box.exec();

	QSettings normalSettings(QCoreApplication::organizationName(),QCoreApplication::applicationName());
	if(normalSettings.value("settings/rememberThreads").toBool())
		saveAllThreadsToRegistry(normalSettings);
}
